/**  
 * All rights Reserved, Designed By www.richfit.com
 * @Title:  MutiDBConfig.java   
 * @Package cn.eonshine.base.config   
 * @Description:    TODO
 * @author: lanjf2000
 * @date:   2018-10-19 08:51:57
 * @version V1.0 
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
package cn.eonshine.base.config;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.google.common.collect.Maps;

import cn.eonshine.base.config.mulitdb.DataSourceEnum;
import cn.eonshine.base.config.mulitdb.MultipleDataSource;

/**   
 * @ClassName:  MutiDBConfig   
 * @Description:TODO
 * @author: lanjf2000
 * @date:   2018-10-19 08:51:57
 *     
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
@Configuration
public class MutiDBConfig {

	@Bean(name = "lsms")
	@ConfigurationProperties(prefix = "spring.datasource.druid.lsms")
	public DataSource lsms() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean(name = "inf")
	@ConfigurationProperties(prefix = "spring.datasource.druid.inf")
	public DataSource inf() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean(name = "securitybackup")
	@ConfigurationProperties(prefix = "spring.datasource.druid.securitybackup")
	public DataSource security() {
		return DruidDataSourceBuilder.create().build();
	}

	/**
	 * Dynamic DataSource configuration
	 * 
	 * @return DataSource
	 */
	@Bean
	@Primary
	public DataSource dynamicDataSource(@Qualifier("lsms")DataSource lsms, @Qualifier("inf")DataSource inf, @Qualifier("securitybackup") DataSource securitybackup) {
		MultipleDataSource dynamicDataSource = new MultipleDataSource();
		// Mulit DB Setting
		Map<Object, Object> targetDataSources = Maps.newHashMap();
		targetDataSources.put(DataSourceEnum.DB1.getValue(), lsms);
		targetDataSources.put(DataSourceEnum.DB2.getValue(), inf);
		targetDataSources.put(DataSourceEnum.DB3.getValue(), securitybackup);
		dynamicDataSource.setTargetDataSources(targetDataSources);
		// default db
		dynamicDataSource.setDefaultTargetDataSource(securitybackup);

		return dynamicDataSource;
	}
}
